package cn.parllay.dingdangproject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.parllay.dingdangproject.R;
import cn.parllay.dingdangproject.bean.Properties;
import cn.parllay.dingdangproject.bean.PropertiesResult;
import cn.parllay.dingdangproject.global.API;
import cn.parllay.dingdangproject.global.QueryString;
import cn.parllay.dingdangproject.utils.DensityUtil;
import cn.parllay.dingdangproject.utils.GlideUtils;
import cn.parllay.dingdangproject.utils.NetWorkUtils;

import static cn.parllay.dingdangproject.DingDangApplication.getContext;

/**
 * Created by sunny on 2017/10/19.
 */

public class CustomerInfoActivity extends BaseActivity {

    @BindView(R.id.tv_customerName)
    TextView tv_customerName;

    @BindView(R.id.tv_nickName)
    TextView tv_nickName;

    @BindView(R.id.tv_gender)
    TextView tv_gender;

    @BindView(R.id.tv_mobile)
    TextView tv_mobile;

    @BindView(R.id.tv_wechat)
    TextView tv_wechat;

    @BindView(R.id.tv_address)
    TextView tv_address;

    @BindView(R.id.tv_isVip)
    TextView tv_isVip;

    @BindView(R.id.ll_tags)
    LinearLayout ll_tags;

    @BindView(R.id.tv_colorName)
    TextView tv_colorName;

    @BindView(R.id.tv_styleName)
    TextView tv_styleName;

    @BindView(R.id.tv_sizeName)
    TextView tv_sizeName;

    @BindView(R.id.tv_total_amount)
    TextView tv_total_amount;

    @BindView(R.id.tv_isNative)
    TextView tv_isNative;

    @BindView(R.id.iv_headImage)
    ImageView iv_headImage;

    private static final String TAG = "CustomerInfoActivity";
    private String mContactId;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_customer_info);

        ButterKnife.bind(this);
        mContactId = getIntent().getStringExtra(QueryString.CONTACT_ID);
    }

    @Override
    public void onResume() {
        super.onResume();

        getCustomerProperties();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(getPropertiesListener != null) {
            getPropertiesListener = null;
        }
    }



    //从服务器获取客户基本信息
    private void getCustomerProperties() {
        HashMap<String, String> params = new HashMap<>();
        params.put(QueryString.CONTACT_ID, mContactId);
        NetWorkUtils.doGET(API.getCustomer(), params, PropertiesResult.class, getPropertiesListener);
    }

    private NetWorkUtils.OnRequestListener getPropertiesListener = new NetWorkUtils.OnRequestListener<PropertiesResult>() {
        @Override
        public void onSuccess(PropertiesResult propertiesResult) {
            List<Properties> propertiesList = propertiesResult.getData();

            if (propertiesList == null || propertiesList.size() == 0) {
                return;
            }
            //绑定数据
            Properties properties = propertiesList.get(0);
            bindData(properties);

        }

        @Override
        public void onError(String errorMsg) {

        }
    };

    //绑定数据
    public void bindData(Properties properties) {

        if (properties == null) {
            return;
        }
        GlideUtils.loadCircleImage(this, GlideUtils.checkUrl(properties.getHeadImage()), iv_headImage);
        tv_customerName.setText(properties.getCustomerName());
        tv_nickName.setText(properties.getNickname());
        tv_gender.setText(properties.getGender());
        tv_mobile.setText(properties.getMobile());
        tv_wechat.setText(properties.getWechat());
        tv_address.setText(properties.getAddress());
        tv_total_amount.setText(properties.getTotalAmount() + "");
        String tags = properties.getTags();
        addTextViewsByTags(ll_tags, tags);
        String isVip = properties.getIsVip() == 1 ? "是" : "否";
        tv_isVip.setText(isVip);
        tv_colorName.setText(properties.getColorName());
        tv_styleName.setText(properties.getStyleName());
        tv_sizeName.setText(properties.getSizeName());
        String isNative = "未知";
        if (properties.getIsNative() == 0) {
            isNative = "未知";
        }
        if (properties.getIsNative() == 1) {
            isNative = "是";
        }
        if (properties.getIsNative() == 2) {
            isNative = "否";
        }
        tv_isNative.setText(isNative);


    }

    private void addTextViewsByTags(LinearLayout linearLayout, String tagString) {

        linearLayout.removeAllViews();

        if (tagString == null || "".equals(tagString)) {
            return;
        }
        List<String> tags = splitTags(tagString);
        for (int i = 0; i < tags.size(); i++) {
            TextView textView = new TextView(getContext());
            textView.setTextColor(getContext().getResources().getColor(R.color.white));
            textView.setBackgroundResource(R.drawable.shape_tags_normal_white);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            if (i > 0) {
                layoutParams.leftMargin = DensityUtil.dip2px(getContext(), 5);
            }
            int padding = DensityUtil.dip2px(getContext(), 3);
            textView.setPadding(padding, padding, padding, padding);
            textView.setText(tags.get(i));
            linearLayout.addView(textView, layoutParams);

        }
    }

    private List<String> splitTags(String tags) {

        String[] split = tags.split("\\^");
        return Arrays.asList(split);
    }

    public static void actionStart(Context context, String contactId) {

        Intent intent = new Intent(context, CustomerInfoActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(QueryString.CONTACT_ID, contactId);
        context.startActivity(intent);
    }

}
